The User Presence Controller (attached to Gameboard) exposes all the presence features as well as keep an internal collection of the active users on the board.

Video Walkthrough:

Step-by-Step

The Gameboard prefab is always available in your game as long as the SDK has been integrated. In order to access it from our custom Mono Behavior we just need to find it by its tag.

    GameObject gameboardObject = GameObject.FindWithTag("Gameboard");

With access to our Gameboard object we can then move on to obtain a reference to the UserPresence controller. This is the controller that handles all the functions related to User Presence.

We will first need to define a reference to the controller to make it available throughout our script:

    UserPresenceController userPresenceController;

With our listener defined, we can now proceed to listen for user presence event. In our case we'll have a function called OnUserPresence handle this events.

We also want to make sure to initialize any users that were connected to the gameboard before the game started up, we can do this by checking if the userPresenceController is initialized. If it is initialized we will call OnUserPresenceControllerInitialized right away, and if it isn't we want to subscribe to the UserPresenceControllerInitialized event to fire off the OnUserPresenceControllerInitialized method whenever the userPresenceController is ready.

    userPresenceController = gameboardObject.GetComponent<UserPresenceController>();

    userPresenceController.OnUserPresence += OnUserPresence;

    if (userPresenceController.IsInitialized)
    {
        OnUserPresenceControllerInitialized();
    }
    else
    {
        userPresenceController.UserPresenceControllerInitialized += OnUserPresenceControllerInitialized;
    }

Now that we are able to receive the user presence events all that remains is to handle them in the game.

Here we process any user presence updates in OnUserPresences, where any users that were around on initialization will be handled in OnUserPresenceControllerInitialized.

    void OnUserPresence(GameboardUserPresenceEventArgs userPresence)
    {
        // Handle changes in User Presence here.
    }

    void OnUserPresenceControllerInitialized()
    {
        foreach (var user in userPresenceController.Users.Values)
        {
            // Initialize existing users here
        }
    }

GameboardUserPresenceEventArgs useful properties

Although we are working in a managed environment it is always a good idea to clean the listeners when no longer needed.

    void OnDestroy()
    {
        userPresenceController.OnUserPresence -= OnUserPresence;
        userPresenceController.UserPresenceControllerInitialized -= OnUserPresenceControllerInitialized;
    }

This section include the entire code in one single, easy to copy section.

    UserPresenceController userPresenceController;

    void Start()
    {
        GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
        userPresenceController = gameboardObject.GetComponent<UserPresenceController>();

        userPresenceController.OnUserPresence += OnUserPresence;

        if (userPresenceController.IsInitialized)
        {
            OnUserPresenceControllerInitialized();
        }
        else
        {
            userPresenceController.UserPresenceControllerInitialized += OnUserPresenceControllerInitialized;
        }
    }

    void OnUserPresence(GameboardUserPresenceEventArgs userPresence)
    {
        // Handle User Presence here.
    }

    void OnUserPresenceControllerInitialized()
    {
        foreach (var user in userPresenceController.Users.Values)
        {
            // Initialize existing users here
        }
    }

    void OnDestroy()
    {
        userPresenceController.OnUserPresence -= OnUserPresence;
        userPresenceController.UserPresenceControllerInitialized -= OnUserPresenceControllerInitialized;
    }